# 实现发送一张图片给我定制化模型的web接口得到图片识别结果

import base64,json,requests

def detect_rubbish(img):


    #定义自己训练好的模型的web接口的url
    request_url = 'https://aip.baidubce.com/rpc/2.0/ai_custom/v1/classification/lajifl'

    #定义自己的access_token
    access_token = '24.257a0d822d12f028923e94f818ed8825.2592000.1603606798.282335-17193409'


    #设置请求头告诉服务强端程序我发给你的是json格式数据
    header = {
        'Content-Type':'application/json'
    }

    #将要识别的图片转换为base64编码字符串

    # with open('static/images/glass35.jpg','rb') as f:
    #     data = f.read()  #返回字节类型
    #     myencode = base64.b64encode(data) #返回base64字节编码
    #     # print(myencode)
    #     encodestr = str(myencode,encoding='utf-8') #字节转换字符串
    #
    #
    #
    # # 定义post请求发送的body参数
    # data ={
    #     "image": encodestr,
    #     "top_num": 7
    # }
    data = {
        "image":img,
        "top_num":7
    }

    #转换为json字符串
    body = json.dumps(data).encode('utf-8')

    #拼接url
    request_url = request_url+"?access_token="+access_token


    #发起post请求把body参数发送给服务器端程序
    response=requests.post(request_url,body,headers=header)

    result=None
    if response.status_code == 200:
        result=response.json()
    print(result)
    return result


if __name__ == '__main__':
    print(detect_rubbish())

